﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Enter.Admin.Utils;
using Microsoft.Extensions.Logging;
using Enter.Admin.Entities;
using AutoMapper;
using Enter.Admin.Models;
using Enter.Admin.Dtoes;
using Enter.Admin.Enums;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using System.IO;
using FluentValidation.AspNetCore;

namespace Enter.Admin.Controllers
{
    public class LoginController : BaseController
    {
        private readonly IConfiguration _configuration;
        private readonly Upload _upload;
        private readonly Mail _mail;

        public LoginController(ILogger<LoginController> logger, IFreeSql fsql, IMapper mapper, IConfiguration configuration, Upload upload, Mail mail) : base(logger, fsql, mapper)
        {          
            _configuration = configuration;
            _upload = upload;
            _mail = mail;
        }
        
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(UserLoginModel model)
        {
            // ModelState.IsValid == true 验证通过;
            //var s = ModelState.IsValid;
            // @Html.AntiForgeryToken()
            //if (HttpContext.Session.Get<DateTime>(SessionKeyTime) == default)
            //{
            //    HttpContext.Session.Set<DateTime>(SessionKeyTime, currentTime);
            //}
            // @HttpContext.Session.GetString(IndexModel.SessionKeyName)
            // @using Microsoft.AspNetCore.Http
            var md5_password = Crypto.Md5(model.Password);
            var user = _fsql.Select<Employ>().Include(t => t.Role)
                        .Where(t => t.UserName == model.UserName && t.Password == md5_password && t.IsDeleted == false).First();
            if (user != null && user.State == (int)EnumEmployState.正常)
            {
                SetSession(user);
                //HttpContext.Session.Clear();
                return Json(new ResponseResult<string>(true, "登陆成功."));
            }
            return Json(new ResponseResult<string>(false, "用户名或密码错误."));
        }


        public ActionResult Logout()
        {
            HttpContext.Session.Clear();
            return RedirectToAction(nameof(Index));
        }

        // GET: Login/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Login/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Login/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Login/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Login/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Login/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}
